/**
 * Kernel Monkeys
 * Andrew Boguk
 * Sergiy Kulyk
 * Nicholas Swartzendruber
 */

/*
 * keyboard.c
 *
 * Makes the physical keyboard a IO interrupt in
 * the program.
 *
 * @Author: Andrew Boguk
 * @Edited by: Nicholas Swartzendruber
 */

#include <pthread.h>

//Start         Header

pthread_t* keyboard_start();
void keyboard_in(void (*func_ptr)());
void test_print_key(void);

//End           Header
/*
int main() {
        pthread_t pt = *(keyboard_start(&test_print_key));
        sleep(5);
        printf("\n%d\n",1+2);
        sleep(5);
        printf("\nEnd\n");
}
*/

/**
 * Starts the keyboard listener on a separate thread.
 *
 * @return Resulting thread that the keyboard listener is on.
 */
pthread_t* keyboard_start(void (*func_ptr))
{
        pthread_t thread;
        pthread_create(&thread, NULL, &keyboard_in, func_ptr);

        return &thread;
}

/**
 * Loops for character return, when detected will run
 * function in CPU to handle IO interrupts.
 *
 * @param *func_ptr CPU-function being called to service interrupt.
 */
void keyboard_in(void (*func_ptr)())
{
        while(1)
        {
                (void) getchar();
                (func_ptr)();
        }
}

void test_print_key(){
        printf("This is a test print to simulate a key triggering a function.");
}
